package com.wchat.serviceimpl;

import com.alibaba.fastjson.JSONObject;
import com.wchat.dataobject.MyMessage;
import com.wchat.repository.MyMessageRepository;
import com.wchat.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private  MyMessageRepository messageRepository;

    @Override
    public void save(String message, String userId,String toUserId) {

        MyMessage myMessage = new MyMessage();
        myMessage.setDate(new Date());
        myMessage.setFromUserId(Integer.parseInt(userId));
        myMessage.setMessageContent(JSONObject.parseObject(message).getString("textMessage"));
        myMessage.setUnReadNum(1);
        myMessage.setUserId(Integer.parseInt(toUserId));
         messageRepository.save(myMessage);

    }
}
